﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using TracNghiem.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;

namespace TracNghiem.Areas.Admin.Models
{
    public class Test_AnswerModel
    {
        public string id { get; set; }
        public string name { get; set; }
        public string status { get; set; }
        public string cau_hoi_id { get; set; }
        public SelectList service_id { get; set; }

        public List<Test_AnswerModel> getTest_AnswerByCauHoi(string cau_hoi_id)
        {
            List<Test_AnswerModel> test_answer = new List<Test_AnswerModel>();
            string sql = "select * from cau_traloi where cau_hoi_id=" + cau_hoi_id + " order by vi_tri";
            OracleDataReader odr = General.Lib_GetDataReader(sql);
            while (odr.Read())
            {

                test_answer.Add(new Test_AnswerModel
                {
                    id = odr["id"].ToString(),
                    name = odr["name"].ToString(),
                    status = odr["status"].ToString() == "1" ? "Bình thường" : "Khóa",
                    cau_hoi_id = odr["cau_hoi_id"].ToString()
                });
            }
            General.conn_global.Close();
            return test_answer;
        }
        public void addTest_Answer(Test_AnswerModel ts)
        {

            string sql = "INSERT INTO cau_traloi";
            sql += " select nvl((select max(id)+1 from cau_traloi),1),'" + ts.name + "'," + ts.status + "'," + cau_hoi_id + " from dual";
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }
        public void EditTest_Answer_Delete(string id_array, string cauhoi_id)
        {
            string sql = "Delete from cau_traloi where id not in (" + id_array + ") and cau_hoi_id=" + cauhoi_id;
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }
        public int checkSubItem(string id_array, string cauhoi_id)
        {
            string sql = "SELECT id FROM bai_thi a,table(a.ket_qua) b ";
            sql += "where cau_tl_id in (select id from cau_traloi where id not in("+id_array+") and cau_hoi_id="+cauhoi_id+")";
            General.conn_global.Close();
            return General.Lib_CheckExist(sql);

        }
        public void EditTest_AnswerEdit(string id, string cau_tra_loi, string status)
        {
            string sql = "UPDATE cau_traloi SET name=trim('" + cau_tra_loi + "'),status=" + status + " where id=" + id;
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }
        public void EditTest_AnswerAdd(string cau_tra_loi, string status, string cauhoi_id)
        {
            string sql = "INSERT INTO cau_traloi";
            sql += " select nvl((select max(id)+1 from cau_traloi),1),trim('" + cau_tra_loi + "')," + status + "," + cauhoi_id + ",(select max(vi_tri)+1 from cau_traloi where cau_hoi_id="+cauhoi_id+") from dual";
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }

    }
}